package com.chii.user.controller;

import com.chii.user.po.ResponeInfo;
import com.chii.user.po.ResultCode;
import com.chii.user.po.Role;
import com.chii.user.po.User;
import com.chii.user.service.RoleService;
import com.chii.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "UpmsController", tags = {"用户和权限操作"})
@RestController
public class UpmsController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private UserService userService;

    @ApiOperation(value = "根据用户名获取用户对象信息")
    @RequestMapping(value = "/getUserByUsername/{username}", method = RequestMethod.GET)
    public ResponeInfo getUserByUsername(@PathVariable String username) throws Exception {
        ResponeInfo responeInfo = new ResponeInfo();
        User user = userService.getUser(username);
        if (user == null) {
            responeInfo.failure("没有" + username + "用户");
        } else {
            responeInfo.success(user);
        }
        return responeInfo;
    }

    @ApiOperation(value = "根据用户ID获取角色列表信息")
    @RequestMapping(value = "/getRoleByUserId/{userId}", method = RequestMethod.GET)
    public ResponeInfo getRoleByUserId(@PathVariable Integer userId) throws Exception {
        ResponeInfo responeInfo = new ResponeInfo();
        List<Role> roles = roleService.getByUserId(userId);
        if (roles == null) {
            responeInfo.failure("没有" + userId + "的权限");
        } else {
            responeInfo.success(roles);
        }
        return responeInfo;
    }

    @ApiOperation(value = "添加用户")
    @RequestMapping(value = "/user", method = RequestMethod.POST)
    public ResponeInfo insertNewUser(@RequestParam("username") String username, @RequestParam("password") String password) throws Exception {
        ResponeInfo responeInfo = new ResponeInfo();
        Role role = roleService.getRole("user");
        int res = userService.insertNewUser(username, password, role.getId());
        if (res == 0) {
            return new ResponeInfo(ResultCode.USER_ACCOUNT_ALREADY_EXIST);
        }
        responeInfo.setMsg("注册成功");
        return responeInfo;
    }


    @ApiOperation(value = "添加用户权限")
    @RequestMapping(value = "/userRole", method = RequestMethod.POST)
    public ResponeInfo insertRole(@RequestParam("userId") Integer userId,
                                  @RequestParam("roleName") String roleName) throws Exception {
        ResponeInfo responeInfo = new ResponeInfo();
        Role role = roleService.getRole(roleName);
        int res = userService.insertNewRoleForUser(userId,role.getId());
        if (res == 0) {
            return new ResponeInfo(ResultCode.COMMON_FAIL);
        }
        responeInfo.setMsg("添加用户权限成功");
        return responeInfo;
    }
    @ApiOperation(value = "删除用户权限")
    @RequestMapping(value = "/userRole", method = RequestMethod.DELETE)
    public ResponeInfo delRole(@RequestParam("userId") Integer userId,
                                  @RequestParam("roleName") String roleName) throws Exception {
        ResponeInfo responeInfo = new ResponeInfo();
        Role role = roleService.getRole(roleName);
        int res = userService.delRoleForUser(userId,role.getId());
        if (res == 0) {
            return new ResponeInfo(ResultCode.COMMON_FAIL);
        }
        responeInfo.setMsg("删除用户权限成功");
        return responeInfo;
    }
}
